package terra.model.object;

import terra.Config;
import terra.definitions.NpcDef;
import terra.model.object.interfaces.Attackable;
import terra.model.object.interfaces.Attacker;
import terra.util.MathUtil;

public class Bird extends NpcObject {
	public Bird(int objectId, NpcDef def) {
		super(objectId, def);
	}

	public Bird() {
	}

	private static final long serialVersionUID = -1982004106418875390L;
	boolean moveLeft = true;
	long lastChangeMoveDirection = System.currentTimeMillis();
	boolean up = true;
	int steps = 0;

	@Override
	public void update(int delta) {
		super.update(delta);
		// dupdateGravity(delta);
		needUpdate();
		if (isDead())
			removeCooldown -= delta;
		if (lastChangeMoveDirection < System.currentTimeMillis()) {
			moveLeft = MathUtil.rnd.nextBoolean();
			lastChangeMoveDirection = System.currentTimeMillis() + 10000L
					+ MathUtil.rnd.nextInt(50000);
		}
		if (up) {
			if (steps < 32)
				setY(getY() - 0.5f);
			else {
				up = !up;
				steps = 0;
			}
		} else {
			if (steps < 32)
				setY(getY() + 0.5f);
			else {
				up = !up;
				steps = 0;
			}
		}
		steps++;

		if (moveLeft) {
			if (!isCollision2(getX() - 64f / 1000f * delta, getY()))
				setX(getX() - 64f / 1000f * delta);
			else {
				moveLeft = false;
				lastChangeMoveDirection = System.currentTimeMillis() + 10000L
						+ MathUtil.rnd.nextInt(50000);
			}
			if (isCollision2(getX() - Config.tileSize * 2, getY()
					+ Config.tileSize))
				setY(getY() - 128f / 1000f * delta);
		} else {
			if (!isCollision2(getX() + 64f / 1000f * delta, getY()))
				setX(getX() + 64f / 1000f * delta);
			else {
				moveLeft = true;
				lastChangeMoveDirection = System.currentTimeMillis() + 10000L
						+ MathUtil.rnd.nextInt(50000);
			}
			if (isCollision2(getX() + Config.tileSize * 2, getY()
					+ Config.tileSize))
				setY(getY() - 128f / 1000f * delta);
		}

//		needUpdate();
	}

	@Override
	public void onHit(Attacker damager, float damage) {
		super.onHit(damager, damage);
		getAi().isAttacked(damager);
	}

	@Override
	public void objectChanged(GObject object) {
	}

	@Override
	public void hit() {
	}

	@Override
	public int getDamage() {
		return 0;
	}

	@Override
	public void onKill(Attackable target) {
	}

}
